package com.muqi.app.qlearn.student.ui;

import static nl.qbusict.cupboard.CupboardFactory.cupboard;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.easemob.easeui.widget.EaseSidebar;
import com.muqi.app.qlearn.adapter.MainListAdapter;
import com.muqi.app.qlearn.student.BaseStudentActivity;
import com.muqi.app.qlearn.student.R;

import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.db.MyClassStudent;
import com.muqi.app.user.widget.QuickIndexBar;
import com.muqi.app.utils.PinYinUtil;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainListActivity extends BaseStudentActivity implements
		OnClickListener, OnItemClickListener {
	private TextView mTv_title;
	private ListView listView;
	private TextView tvCenter;


	private List<MyClassStudent> dbStudents;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_list);
	}

	@Override
	protected void onInit() {
		super.onInit();
		mTv_title = (TextView) findViewById(R.id.tv_title);
		listView = (ListView) findViewById(R.id.list);
		tvCenter=(TextView) findViewById(R.id.floating_header);
		listView.setOnItemClickListener(this);
	
	    QuickIndexBar bar = (QuickIndexBar) findViewById(R.id.sidebar1);
	    bar.setListener(new QuickIndexBar.OnLetterUpdateListener() {
	            @Override
	            public void onLetterUpdate(String letter) {
	            	 tvCenter.setVisibility(View.VISIBLE);
	                 tvCenter.setText(letter);
	                //根据字母定位ListView，找到集合中第一个以letter为拼音首字母的对象，得到索引
	                for (int i = 0; i < dbStudents.size(); i++) {
	                   MyClassStudent classStudent = dbStudents.get(i);
	                    String l = classStudent.initialLetter;
	                    if (TextUtils.equals(letter, l)) {
	                        //匹配成功
	                        listView.setSelection(i);
	                        break;
	                    }
	                }
	            }

				@Override
				public void onCenterTvHide() {
					tvCenter.setVisibility(View.GONE);
					
				}
	        });

		if (myChildren != null && myChildren.size() > 0) {
			if (myChildren.size() > 1) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.btn_down_white);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
				mTv_title.setCompoundDrawables(null, null, drawable, null);
				mTv_title.setOnClickListener(this);
			}
			mTv_title.setText(myChildren.get(0).class_name);

		} else {
			mTv_title.setText("班级通讯录");
		}

	}

	@Override
	protected void onData() {
		super.onData();
		initData(mSpUtil.getChildClassId());
	}

	private void initData(String classId) {
		showLoading();
		mTv_title.setText(mSpUtil.getClassName());
		dbStudents = cupboard().withDatabase(sqlDb).query(MyClassStudent.class)
				.withSelection("class_id = ?", classId).list();
		if (dbStudents != null && dbStudents.size() > 0) {
			for (MyClassStudent myClassStudent : dbStudents) {
				if (myClassStudent.student_name != null
						&& !myClassStudent.equals("")) {
					myClassStudent.initialLetter = PinYinUtil.getPinyin(
							myClassStudent.student_name).substring(0, 1);
				} else {
					myClassStudent.initialLetter = "";
				}

			}
			Collections.sort(dbStudents, new Comparator<MyClassStudent>() {

				@Override
				public int compare(MyClassStudent lhs, MyClassStudent rhs) {
					return lhs.initialLetter.compareTo(rhs.initialLetter);
				}
			});
			hideLoading();
			listView.setAdapter(new MainListAdapter(mContext,
					R.layout.ease_custom_contact, dbStudents));
			
		}

	}

	public void onBack(View v) {
		this.finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_title:
			showChildrenWindow(v);
			break;
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Intent intent = new Intent(mContext, ContactsPersonDetailActivity.class);
		intent.putExtra(ContactsPersonDetailActivity.IM_OR_MOBILE_OR_USERID,
				dbStudents.get(position).student_im_user_id);
		startActivity(intent);

	}

	@Override
	protected void onChoiceChildResult(ChildBean myChild) {
		initData(myChild.class_id);

	}
	

}
